﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using _404park.Core.Content;
using _404park.Core.DataInterface;
using _404park.Utilities;

namespace _404park.Data.Content
{
    public class KeywordRepository : IKeywordRepository
    {
        private static readonly string _directoryName = ConfigurationManager.AppSettings["keywordsDataDirectory"];
        private static readonly string _fileName = ConfigurationManager.AppSettings["keywordsFileName"];
        private static List<Keyword> _keywords = new List<Keyword>();
        private static FileSystemWatcher _watcher = new FileSystemWatcher();

        static KeywordRepository()
        {
            //StartMonitor();
        }

        public IList<Keyword> GetAllKeywords()
        {
            if (_keywords != null && _keywords.Count() > 0)
                return _keywords;
            else
            {
                RefreshKeywords();
            }
            return _keywords;
        }

        public static IList<Tag> PopulateKeywords()
        {
            var keywordFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _directoryName, _fileName);
            if (!File.Exists(keywordFilePath))
                return null;

            IList<Tag> tags = new List<Tag>();

            string[] keywordData = File.ReadAllLines(keywordFilePath);
            foreach (string line in keywordData)
            {
                string[] keywords = line.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
                if (keywords.Count() == 0)
                    continue;

                var tag = new Tag()
                {
                    Name = keywords[0]
                };

                for (int i = 1; i < keywords.Count(); i++)
                {
                    tag.AddKeyword(new Keyword(){
                        Word = keywords[i].Trim(),
                        Rating = 1,
                    });
                }

                tags.Add(tag);
            }

            return tags;
        }

        public static void StartMonitor()
        {
            _watcher.Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _directoryName);
            _watcher.NotifyFilter = NotifyFilters.FileName
                | NotifyFilters.LastAccess
                | NotifyFilters.LastWrite
                | NotifyFilters.Size
                | NotifyFilters.DirectoryName;
            _watcher.Filter = "*";
            _watcher.IncludeSubdirectories = false;

            _watcher.Changed += new FileSystemEventHandler(OnChanged);
            _watcher.EnableRaisingEvents = true;
        }

        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            RefreshKeywords();
        }

        private static void RefreshKeywords()
        {
            var tags = PopulateKeywords();
            if (tags == null) return;
            foreach (var tag in tags)
            {
                _keywords.AddRange(tag.KeywordList);
            }
        }
    }
}
